﻿using System.Collections;
using UnityEngine;
namespace UGUIMan
{
    public class Ammo:MonoBehaviour
    {
        public float hp=HP.current;
        public GameObject[] list;
        public bool[] flags;
        public Vector3 deltapos;
        public Vector3 deltaposmax=new Vector3(0,100,0);
        private void Awake()
        {
            
            flags = new bool[list.Length];
            for(int i=0 ;i<list.Length ; i++)
            {
                list[i].AddComponent<Am>();
                flags[i] = false;
            }
        }
        private void Update()
        {
            hp = HP.current;
            if (list.Length == Armas.ArmasMax)
            {
                for(int i=0 ;i<list.Length ; i++)
                {
                    if (i < Armas.ArmasCurrent)
                    {
                        if (!flags[i])
                        {
                            flags[i] = true;
                            Dragup(list[i]);
                        }
                    }
                    else
                    {
                        if (flags[i])
                        {
                            flags[i] = false;
                            Dragdown(list[i]);
                        }
                    }
                }
            }
        }
        private void Dragup(GameObject g)
        {
            StartCoroutine(Upcon(g));
        }
        IEnumerator Upcon(GameObject g)
        {
            yield return new WaitForSeconds(0.01f);
            if (g.GetComponent<Am>().Timer > 0)
            {
                g.GetComponent<Am>().delta += new Vector3(0, 1, 0);
                
                g.transform.GetChild(0).GetChild(1).transform.localPosition = g.GetComponent<Am>().pos + g.GetComponent<Am>().delta;
                g.transform.GetChild(0).GetChild(0).transform.localPosition =
                g.transform.GetChild(0).GetChild(1).transform.localPosition + new Vector3(0, -10, 0);
                g.GetComponent<Am>().Timer -= Time.deltaTime;
                StartCoroutine(Upcon(g));
            }
            else
            {
                g.transform.GetChild(0).GetChild(1).transform.localPosition = g.GetComponent<Am>().pos;
                g.GetComponent<Am>().delta = new Vector3();
                g.GetComponent<Am>().Timer = 0;
            }
        }

        private void Dragdown( GameObject g )
        {
            StartCoroutine(Downcon(g));
        }
        IEnumerator Downcon( GameObject g )
        {
            yield return new WaitForSeconds(0.01f);
            if (g.GetComponent<Am>().Timer < 1)
            {
                g.GetComponent<Am>().delta-=new Vector3(0,1,0);

                g.transform.GetChild(0).GetChild(1).transform.localPosition = g.GetComponent<Am>().pos+ g.GetComponent<Am>().delta;
                g.transform.GetChild(0).GetChild(0).transform.localPosition =
                g.transform.GetChild(0).GetChild(1).transform.localPosition + new Vector3(0, -10, 0);
                g.GetComponent<Am>().Timer += Time.deltaTime;
                StartCoroutine(Downcon(g));
            }
            else
            {
                g.GetComponent<Am>().delta = g.GetComponent<Am>().deltaMax;
                g.transform.GetChild(0).GetChild(1).transform.localPosition = g.GetComponent<Am>().pos+g.GetComponent<Am>().deltaMax;
                g.GetComponent<Am>().Timer = 1;
            }
        }
    }
    public class Am:MonoBehaviour
    {
        public Vector3 pos;
        public Vector3 delta;
        public Vector3 deltaMax=new Vector3(0,-100,0);
        public float Timer = 1;

        private void Awake()
        {
            pos = transform.GetChild(0).localPosition;
        }


    }
}